app.controller('BusConfigModuleCtrl', function ($scope, ajax, toaster) {

    $scope.reload = function () {
        ajax.call(new Req.GET({
            resource: 'config/module/bs'
        })).then(function (result) {
            $scope.list = result.result;
            for (var i = 0; i < $scope.list.length; i++) {
                var key = $scope.list[i].key;
                if (key == "bus.refund.rule") {
                    $scope.refundRule = $scope.list[i];//退票规则
                    continue;
                } else if (key == "bus.order.points") {
                    $scope.points = $scope.list[i]; //积分配置
                    continue;
                }else if(key == "bus.order.days"){
                    $scope.days = $scope.list[i]; //巴士票预售天数
                    continue;             	
                }else if(key == "bus.order.times"){
                    $scope.times = $scope.list[i]; //发车前关闭售票时间
                    continue;             	
                }else if(key == "bus.timeout.pay"){
                    $scope.pay = $scope.list[i]; //订单支付超时时间
                    continue;             	
                }else if(key == "bus.refund.times"){
                    $scope.refundTime = $scope.list[i]; //发车前关闭退票时间
                    continue;             	
                }else if(key == "bus.report.days"){
                	$scope.reportDay = $scope.list[i]; //报班天数
                }else if(key == "bus.report.times"){
                	$scope.reportTime = $scope.list[i]; //报班时间
                }else if(key == "bus.service.protocol"){
                	$scope.protocol = $scope.list[i]; //服务协议
                }else if(key == "bus.insurance.inform"){
                	$scope.insurance = $scope.list[i]; //保险说明
                }else if(key == "bus.order.inform"){
                	$scope.order = $scope.list[i]; //订票需知
                }
            }
        }, function () { });
    };

    var inserter = new Req.POST({ resource: $scope.resource });
    var updater = new Req.PUT({ resource: $scope.resource });


    $scope.reload();
    
    $scope.fi = [];
    //班次筛选时段时间
    $scope.loadFilter = function () {
        var url = "bus.shift.filter";
        ajax.call(new Req.GET({
            resource: 'config/module/value?key=' + url
        })).then(function (result) {
            $scope.fi = result.result;
            var fi = $scope.fi.value;
            var strs = fi.split(",");
            $scope.total = [];
            for (var i = 0; i < strs.length; i++) {
                var sp = strs[i].split("@@");
                $scope.total.push(sp);
            }
        }, function () { });
    };
    $scope.loadFilter();

    $scope.fg = function (total) {
      if ($scope.fileForm.$valid){
        var strs = total;
        var str = "";
        var flag = 0;
        for (var i = 0; i < strs.length; i++) {
            flag++;
            if (flag == strs.length) {
                str += strs[i][0] + '@@' + strs[i][1];
            } else {
                str += strs[i][0] + '@@' + strs[i][1] + ',';
            }
        }
        $scope.fi.value = str;
        updater.fields = $scope.fi;
        updater.id = $scope.fi.id;
        updater.resource = 'config/module/bk/update';
        ajax.call(updater).then(function (result) {
            if (result.code == 200) {
                toaster.pop('success', '成功', '修改信息成功。');
            } else {
                toaster.pop('error', '错误', '修改信息失败！');
            }
        }, function () {
            toaster.pop('error', '错误', '修改信息失败！');
        });
      }else{
			toaster.pop('warning', '提示', '填写格式错误或内容长度超出，请修改后再提交！');
			return;
      }
    };

    $scope.re = function () {
        var url = "bus.shift.filter";
        ajax.call(new Req.GET({
            resource: 'config/module/value?key=' + url
        })).then(function (result) {
            $scope.fi = result.result;
            var fi = $scope.fi.defaultValue;
            var strs = fi.split(",");
            $scope.total = [];
            for (var i = 0; i < strs.length; i++) {
                var sp = strs[i].split("@@");
                $scope.total.push(sp);
            }
            $scope.reset($scope.fi);
        }, function () { });
    };

    //保存修改
    $scope.save = function (module) {
		if(module.value==undefined){
			toaster.pop('warning', '提示', '填写格式错误或内容长度超出，请修改后再提交！');
			return;
		}
		if(module.key=="bus.report.times"&&module.value>24){
			toaster.pop('warning', '提示', '当天报班时间不可大于24小时！');
			return;
		}
        updater.fields = module;
        updater.id = module.id;
        updater.resource = 'config/module/bk/update';
        ajax.call(updater).then(function (result) {
            if (result.code == 200) {
                toaster.pop('success', '成功', '修改信息成功。');
                $scope.reload();
            } else {
                toaster.pop('error', '错误', '修改信息失败！');
            }
        }, function () {
            toaster.pop('error', '错误', '修改信息失败！');
        });
    };

    //恢复默认值
    $scope.reset = function (module) {
        inserter.resource = 'config/module/reset?key=' + module.key;
        ajax.call(inserter).then(function (result) {
            if (result.code == 200) {
                toaster.pop('success', '成功', '恢复默认值成功。');
                $scope.reload();
            } else {
                toaster.pop('error', '错误', '恢复默认失败！');
            }
        }, function () {
            toaster.pop('error', '错误', '恢复默认失败！');
        });
    };
});